<template>
  <gc-dialog
    :title="staffInfo ? '更新员工' : '新增员工'"
    :dialog-visible="true"
    @cancel="handleCancle"
    @ok="handleConfirm"
  >
    <el-form
      ref="formRef"
      label-position="top"
      :model="form"
      :rules="formRules"
    >
      <el-form-item label="用户名" prop="username">
        <el-input type="text" v-model="form.username"  placeholder="请输入" :disabled="isEdit" />
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="!isEdit">
        <el-input type="password" v-model="form.password" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input type="text" v-model="form.nickname" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="角色" prop="roleIds">
        <el-select v-model="form.roleIds" multiple placeholder="请选择角色">
          <el-option
            v-for="item in roleOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            <p>{{ item.name }} <em v-if="item.status !== '1'" style="color: #999; font-size: 12px;">（禁用）</em></p>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="form.gender">
          <el-radio label="1">男</el-radio>
          <el-radio label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="状态" prop="status" v-if="!isEdit">
        <el-radio-group v-model="form.status">
          <el-radio label="1">启用</el-radio>
          <el-radio label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input-number
          step-strictly
          :step="1"
          :min="1"
          v-model="form.age"
          placeholder="请输入" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input type="text" v-model="form.email" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input type="text" v-model="form.tel" placeholder="请输入" />
      </el-form-item>
    </el-form>
  </gc-dialog>
</template>

<script setup lang="ts" name="OperateStaffModal">
import { ref, reactive, watch, computed, type PropType } from 'vue'
import { ruleRequired, ruleMaxLength, IS_CODE, NAME_CN } from '@/utils/rules'
import type { FormInstance, FormRules } from 'element-plus'
import { apiGetRoleList, apiCreateStaff, apiUpdateStaff } from '@/http/service'
import { ElMessage } from 'element-plus'
import type { Staff } from '@/http/type'

interface Role {
  id: number
  name: string
  status: '0' | '1'
}
const props = defineProps({
  staffInfo: {
    type: Object as PropType<null | Staff>,
    default: () => null
  }
})
const emit = defineEmits(['close-modal'])
const formRef = ref<null | FormInstance>(null)
const form = reactive({
  username: '',
  password: '',
  nickname: '',
  gender: null,
  status: null,
  avatar: '',
  email: '',
  tel: '',
  age: null,
  roleIds: []
})
const formRules = reactive<FormRules>({
  username: [ruleRequired('请输入用户名'), ruleMaxLength(16), IS_CODE],
  password: [ruleRequired('请输入用户密码'), ruleMaxLength(64)],
  status: [ruleRequired('请选择状态')],
  nickname: [ruleMaxLength(32), NAME_CN],
  email: [ruleMaxLength(32)],
  tel: [ruleMaxLength(16)],
  roleIds: [ruleRequired('请选择角色')]
})
const roleOptions = ref<Array<Role>>([])
const isEdit = computed(() => {
  return Boolean(props.staffInfo)
})

/**
 * 角色列表查询
 */
async function findRoleList () {
  const data = await apiGetRoleList<Array<Role>>()
  roleOptions.value = data
}
findRoleList()

/**
 * 员工新增编辑form表单提交
 */
async function handleConfirm () {
  const valid = await formRef.value?.validate()
  if (valid) {
    const API = isEdit.value ? apiUpdateStaff : apiCreateStaff
    await API(form)
    ElMessage.success(isEdit.value ? '编辑员工成功' : '新增员工成功')
    emit('close-modal', true)
  }
}

function handleCancle () {
  emit('close-modal')
}

watch(() => props.staffInfo, val => {
  if (!val) return
  const { id, username, nickname, gender, avatar, email, tel, age, roleIds } = val
  Object.assign(form, {
    id, username, nickname, gender, avatar, email, tel, age, roleIds
  })
}, { immediate: true })
</script>

<style scoped lang="scss"></style>
